package cn.onestoplog.currEx.yahoo;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;

import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

import cn.onestoplog.currEx.common.DateUtil;

public class YahooCurrencyConverter extends BaseCurrencyConverter {

	 public float convert(String currencyFrom, String currencyTo) throws IOException {
	        HttpClient httpclient = new DefaultHttpClient();
	        HttpGet httpGet = new HttpGet("http://quote.yahoo.com/d/quotes.csv?s=" + currencyFrom + currencyTo + "=X&f=l1&e=.csv");
	        ResponseHandler<String> responseHandler = new BasicResponseHandler();
	        String responseBody = httpclient.execute(httpGet, responseHandler);
	        httpclient.getConnectionManager().shutdown();
	        return Float.parseFloat(responseBody);
	    }
	    public void convert(CurrencyPair[] currencyPairs) throws Exception {
	        HttpClient httpclient = new DefaultHttpClient();
	        StringBuffer sb = new StringBuffer();
	        for (CurrencyPair currencyPair : currencyPairs) {
	            sb.append("s=").append(currencyPair.getFrom()).append(currencyPair.getTo()).append("=X&");
	        }
	        HttpGet httpGet = new HttpGet("http://quote.yahoo.com/d/quotes.csv?" + sb + "f=l1d1t1&e=.csv");
	        ResponseHandler<String> responseHandler = new BasicResponseHandler();
	        String responseBody = httpclient.execute(httpGet, responseHandler);
	        //System.out.println("responseBody");
	        //System.out.println(responseBody);
	        httpclient.getConnectionManager().shutdown();
	        String[] lines = responseBody.split("\n");
	        if (lines.length != currencyPairs.length) {
	            throw new IllegalStateException("Currency data mismatch");
	        }
	        int i = 0;
	        for (String line : lines) {
	        	String[] parts = line.split(",");
	            CurrencyPair currencyPair = currencyPairs[i++];
	            currencyPair.price = Float.parseFloat(parts[0]);
	            if(parts[1].indexOf("N/A")==-1)
					currencyPair.date = DateUtil.str2Date(parts[1].replaceAll("\"", "")+" "+parts[2].replaceAll("\"", "").trim());
	        }
	        System.out.println();
	    }
	    public static void main(String[] args) {
	        YahooCurrencyConverter ycc = new YahooCurrencyConverter();
	        try {
	            //String[] currencies = new String[] { "USD", "EUR"};
	            String[] currencies = new String[] { "USD","CNY", "EUR","HKD", "GBP", "JPY", "CHF", "CAD", "AUD", "MXN"};
	            HashMap<String, CurrencyPair> datas = ycc.getConversionMatrix(currencies);
//	            System.out.print("    ");
//	            for (int i = 0; i < currencyPairs.length; i++)
//	            {
//	                System.out.print("     " + currencies[i]);
//	            }
//	            System.out.println();
//	            for (int i = 0; i < currencyPairs.length; i++)
//	            {
//	                for (int j = 0; j < currencyPairs.length; j++)
//	                {
//	                    if (j == 0)
//	                    {
//	                        System.out.print(currencies[i] + " ");
//	                    }
//	                    CurrencyPair currencyPair = currencyPairs[i][j];
//	                    if (currencyPair != null)
//	                    {
//	                        System.out.printf("%8.3f", currencyPair.price);
//	                    }
//	                    else
//	                    {
//	                        System.out.print("        ");
//	                    }
//	                }
//	                System.out.println();
//	            }
	            
	            Iterator<String> its = datas.keySet().iterator();
	            while(its.hasNext()){
	            	String key = its.next();
	            	CurrencyPair cp = datas.get(key);
	            	
	            	System.out.println(key + "   " +cp.getPrice() + "   " + cp.getDate());
	            }
	        }
	        catch (Exception e) {
	            e.printStackTrace();
	        }
	    }

}
